package com.lsk.serviceDriverUser.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lsk.internalcommon.DTO.DriverCarBindingRelationship;
import com.lsk.internalcommon.DTO.ResponseResult;
import com.lsk.internalcommon.constant.CommonStatusEnum;
import com.lsk.internalcommon.constant.DriverCarConstants;
import com.lsk.serviceDriverUser.mapper.DriverCarBindingRelationshipMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class DriverCarBindingRelationshipService {
    @Autowired
    private DriverCarBindingRelationshipMapper relationshipMapper;

    public ResponseResult bind(DriverCarBindingRelationship relationship){
        //判断司机车辆是否绑定
        QueryWrapper<DriverCarBindingRelationship> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id",relationship.getDriverId());
        queryWrapper.eq("car_id",relationship.getCarId());
        queryWrapper.eq("bind_state",DriverCarConstants.DRIVER_CAR_BIND);
        Integer count = relationshipMapper.selectCount(queryWrapper);
        if (count>0){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_EXISTS.getCode(),CommonStatusEnum.DRIVER_CAR_BIND_EXISTS.getValue());
        }
        //判断司机是否绑定
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id",relationship.getDriverId());
        queryWrapper.eq("bind_state",DriverCarConstants.DRIVER_CAR_BIND);
        count = relationshipMapper.selectCount(queryWrapper);
        if (count>0){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_BIND_EXISTS.getCode(),CommonStatusEnum.DRIVER_BIND_EXISTS.getValue());
        }
        //判断车辆是否绑定
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("car_id",relationship.getCarId());
        queryWrapper.eq("bind_state",DriverCarConstants.DRIVER_CAR_BIND);
        count = relationshipMapper.selectCount(queryWrapper);
        if (count>0){
            return ResponseResult.fail(CommonStatusEnum.CAR_BIND_EXISTS.getCode(),CommonStatusEnum.CAR_BIND_EXISTS.getValue());
        }

        LocalDateTime now = LocalDateTime.now();
        relationship.setBindingTime(now);
        relationship.setBindState(DriverCarConstants.DRIVER_CAR_BIND);
        relationshipMapper.insert(relationship);
        return ResponseResult.success("");
    }

    public ResponseResult unbind(DriverCarBindingRelationship relationship){
        LocalDateTime now = LocalDateTime.now();
        Map<String, Object> map = new HashMap<>();
        map.put("driver_id",relationship.getDriverId());
        map.put("car_id",relationship.getCarId());
        map.put("bind_state",DriverCarConstants.DRIVER_CAR_BIND);
        List<DriverCarBindingRelationship> relationshipList = relationshipMapper.selectByMap(map);
        if (relationshipList.isEmpty()){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getCode(),CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getValue());
        }

        relationship.setBindingTime(now);
        relationship.setBindState(DriverCarConstants.DRIVER_CAR_BIND);
        relationshipMapper.updateById(relationship);
        return ResponseResult.success("解绑成功");

    }
}
